package ca.eloas.judging.rest.resources;

import ca.eloas.judging.messages.CompetitionListMessage;
import ca.eloas.judging.messages.CompetitionMessage;
import ca.eloas.judging.messages.ScoreListMessage;
import ca.eloas.judging.messages.ScoreMessage;
import ca.eloas.judging.messages.TeamListMessage;
import ca.eloas.judging.messages.TeamMessage;
import ca.eloas.judging.model.CompetitionObject;
import ca.eloas.judging.model.JudgeObject;
import ca.eloas.judging.model.ScoreObject;
import ca.eloas.judging.model.TeamObject;
import ca.eloas.judging.rest.repositories.CompetitionRepository;
import ca.eloas.judging.rest.repositories.ScoreRepository;
import ca.eloas.judging.rest.repositories.TeamRepository;
import ca.eloas.restsupport.operations.LIST;
import ca.eloas.restsupport.utils.BasicResource;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import static ca.eloas.judging.rest.operations.ChildLink.childLink;
import static ca.eloas.judging.rest.operations.CopyToMessage.copyToMessage;
import static ca.eloas.judging.rest.operations.SelfLink.selfLink;

/**
 * @author JP
 */
public class JudgeScoreResource extends BasicResource {


    private final JudgeObject judge;
    private final CompetitionObject competition;

    @Inject
    private ResourceFactory resourceFactory;


    @Inject
    private TeamRepository teams;

    @Inject
    private ScoreRepository scores;

    @AssistedInject
    public JudgeScoreResource(@Assisted JudgeObject j, @Assisted CompetitionObject o) {

        this.judge = j;
        this.competition = o;
    }

    @Path("/")
    @LIST
    @GET
    public TeamListMessage listCompetingTeams() {

        Iterable<TeamObject> i = teams.getTeams(competition);
        return list(i, TeamListMessage.class, TeamMessage.class).runOnList(
                selfLink()
        ).runOnItems(
                copyToMessage(),
                selfLink()
        ).now();
    }

    @Path("/{id}")
    @GET
    public ScoreMessage getScore(@PathParam("id")long id) {


        TeamObject to = teams.getTeam(competition, id);

        if ( to == null ) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        ScoreObject score = scores.getScoreForJudge(to, judge);
        if ( score == null ) {

            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        return get(score, ScoreMessage.class).runAll(

                copyToMessage(),
                selfLink()
        ).now();
    }

    @Path("/{id}")
    @POST
    public ScoreMessage setScore(@PathParam("id")long id, ScoreMessage message) {


        TeamObject to = teams.getTeam(competition, id);

        if ( to == null ) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        ScoreObject score = scores.getScoreForJudge(to, judge);
        if ( score == null ) {

            score = to.score(judge, message.getScore(), message.getComments());
        } else {

            score.setComments(message.getComments());
            score.setScore(message.getScore());
        }

        scores.save(score);

        return post(score, message, ScoreMessage.class)
                .runAllOnOutputMessage(
                        copyToMessage(),
                        selfLink()
                ).now();
    }


}
